define(["angularAMD", "moment"], function (angularAMD, moment) {
    angularAMD.service('B0221Service', ['$rootScope', '$filter', '$translate', 'config', 'MMWService', '$mdDialog', '$calculater', '$mddProvider',
        function ($rootScope, $filter, $translate, config, $MMWService, $mdDialog, $calculater,$mddProvider) {
            /**
             * options : {
             *     machine : {
             *         machine_type
             *         machine_no
             *     }
             *     callback : function
             * }
            **/
            this.init = function (options) {
                //可用模具查詢
                function LoadAppliedAccessory(content, callback){
                    $mddProvider.UseForm('B0221', () => {
                        $MMWService.sendToServer({
                            // uri: $mddProvider.Getfunc('B0221', 'load_applied_acc_info_get'),
                            uri : 'ServicesSTD.Module_ACC.load_applied_acc_info_get',
                            content : content,
                            success : function(data){
                                if(callback)
                                    callback(data);
                            }
                        });
                    })
                }
                //已上機模具資訊查詢
                function LoadOnEquipmentAccessory(content, callback){
                    $MMWService.sendToServer({
                        // uri: $mddProvider.Getfunc('B0221', 'load_on_machine_acc_info_get'),
                        uri : 'ServicesSTD.Module_ACC.load_on_machine_acc_info_get',
                        content : content,
                        success : function(data){
                            //20220208 add by 翰鴻 for M#102969
                            //TargetLocator 為假欄位，避免填入目標儲位時，修改到預設儲位
                            $.each(data, function (i, val) {
                                val.TargetLocator = val.LocatorNo;
                            })
                            if(callback)
                                callback(data);
                        }
                    });
                }
                //模具上機作業
                function LoadAccToEquipment(content, callback){
                    content.pTable = content.pTable;
                    //20220307 modify by 翰鴻 for M#102965
                    var redList = $.grep(content.pTable, function (n, i) {
                        return n.CHANGETOOL == 1;
                    });
                    if(redList.length > 0)
                    {
                        //選擇模治具為最近使用的模治具，是否強制上模？
                        $rootScope.showConfirm($filter('translate')('B0221.msg.force_load_Acc'), function (dialog) {
                            dialog.hide();
                            $mdDialog.serviceDialog('PasswordService', {
                                mode: 'AreaLeader',
                                equipmentno: options.machine.machine_no,
                                feedback: function () {
                                    $MMWService.sendToServer({
                                        // uri: $mddProvider.Getfunc('B0221', 'load_applied_acc_to_machine_process'),
                                        uri: 'ServicesSTD.Module_ACC.load_applied_acc_to_machine_process',
                                        content: content,
                                        success: function (data) {
                                            if (data != false)
                                                $rootScope.showAlert(data);
                                            if (callback)
                                                callback(data);
                                        },
                                        // 20210503 modify by kaiinagami for #85272 - 使用error callback執行畫面重載。
                                        error_callback: function (data) {
                                            if (callback) {
                                                callback(data);
                                            }
                                        }
                                    });
                                }
                            });
                        });
                    }
                    else
                    {
                        $MMWService.sendToServer({
                            // uri: $mddProvider.Getfunc('B0221', 'load_applied_acc_to_machine_process'),
                            uri: 'ServicesSTD.Module_ACC.load_applied_acc_to_machine_process',
                            content: content,
                            success: function (data) {
                                if(data != false)
                                    $rootScope.showAlert(data);
                                if (callback)
                                    callback(data);
                            },
                            // 20210503 modify by kaiinagami for #85272 - 使用error callback執行畫面重載。
                            error_callback: function (data) {
                                if (callback) {
                                    callback(data);
                                }
                            }
                        });
                    }     
                }

                //模具下機作業
                function UnLoadAccFromEquipment(content, callback){
                    content.pTable = content.pTable;
                    $MMWService.sendToServer({
                        // uri: $mddProvider.Getfunc('B0221', 'unload_acc_from_machine_process'),
                        uri : 'ServicesSTD.Module_ACC.unload_acc_from_machine_process',
                        content : content,
                        success : function(data) {
                            if(data.exec_result == 0){
                               $rootScope.showConfirm(data.message, function(dialog){
                                    dialog.hide();
                                    content.enforce = 1;
                                    UnLoadAccFromEquipment(content, callback);
                               }); 
                            }
                            else {
                                if (data.message)
                                {
                                    $rootScope.showAlert(data.message);
                                }
                                if(callback)
                                    callback(data);
                            }
                        },
                        // 20210503 modify by kaiinagami for #85272 - 使用error callback執行畫面重載。
                        error_callback: function(data) {
                            if (callback) {
                                 callback(data);
                            }
                        }
                    });
                }

                //異常下機作業
                function funUnloadAccessory_Scrap(content, callback){
                    $MMWService.sendToServer({
                        // uri: $mddProvider.Getfunc('B0221', 'unload_scrap_acc_from_machine_process'),
                        uri : 'ServicesSTD.Module_ACC.unload_scrap_acc_from_machine_process',
                        content : content,
                        success : function(data){
                            if(data.exec_result == 0){
                                $rootScope.showConfirm(data.message, function(dialog){
                                     dialog.hide();
                                     content.enforce = 1;
                                     funUnloadAccessory_Scrap(content, callback);
                                }); 
                             }else{
                                $rootScope.showToast($filter('translate')('common.msg.success'));
                                if(callback)
                                    callback(data);
                             }
                        }
                    });
                }

                function OpenScanner(callback){
                    $rootScope.OpenScanner({
                        after : function(result){
                            if(callback)
                                callback(result);
                        },
                        exception: function(){
                            //目前裝置無法使用相機
                            $rootScope.showAlert($filter('translate')('B0214.showAlert.column3'));
                        },
                        orientation : 'landscape'
                    });
                }

                function unLoad_accessory_callback(dialog){
                    var isMatch = false;
                    dialog.LoadAccDetails.forEach(function(item){
                        if(item.AccessoryNo == dialog.unLoadAcc_barcode){
                            item.isSelect = true;
                            isMatch = true;
                        }else{
                            item.isSelect = false;
                        }
                    });
                    if(isMatch){
                        dialog.UnLoadAccFromEquipment();
                    }else{
                        dialog.unLoadAcc_barcode = '';
                        $rootScope.showToast($filter('translate')('B0221.msg.no_match_accDetails'), 'error');
                    }
                }

                function load_accessory_callback(dialog){
                    var isMatch = false;
                    dialog.unLoadAccDetails.forEach(function(item){
                        if(item.AccessoryNo == dialog.loadAcc_barcode){
                            item.isSelect = true;
                            isMatch = true;
                        }else{
                            item.isSelect = false;
                        }
                    });
                    if(isMatch){
                        dialog.LoadAccToEquipment();
                    }else{
                        dialog.loadAcc_barcode = '';
                        $rootScope.showToast($filter('translate')('B0221.msg.no_match_accDetails'), 'error');
                    }
                }

                function openExceptionDialog(exception_acc_details, callback){
                    var now_date = new Date();
                    $mddProvider.UseForm('B0221_exception', ()=>{
                        $mdDialog.dialog(
                            'module/moduleB/program/B02/B0221/B0221.exception.html',
                            function (dialog) {
                                return {
                                    css_url:'module/moduleB/program/B02/B0221/B0221.exception.css',
                                    next_date:now_date,
                                    now_date:now_date,
                                    exception_reason_display:'',
                                    // 20200214 modify by WeiTing for M#65970 - 增加叫修原因(call_repair_reason)、直接叫修按鈕(isRepair)。
                                    call_repair_reason_no:'',
                                    call_repair_reason_name:'',
                                    isRepair:false,
                                    _event:{
                                        'exception_reason_display':{
                                            click:()=>{
                                                dialog.loadExceptoinReason()
                                            }
                                        }
                                    },
                                    _fn:{
                                        'Execute' :{
                                            click:()=>{
                                                dialog.confirm()
                                            }
                                        }
                                    },
                                    back:function(){
                                        dialog.hide();
                                    },
                                    loadExceptoinReason:function(){
                                        $MMWService.showQueryListPNIG({
                                            // page_mode: 31,
                                            form_name:'PageNo_23',
                                            key:'page_no',
                                            header: [
                                                {label:$filter('translate')('B0221.exception.acc_exption_reason_c'), code:'page_no'},
                                                {label:$filter('translate')('B0221.exception.acc_exption_reason_n'), code:'page_name'}
                                            ],
                                            setCondition:function(conditions){
                                                conditions.push(
                                                    {column_name:'ReasonType', query_mode:0, value: '0', merge_condition_model:'AND'},
                                                    {column_name:'ISSUESTATE', query_mode:0, value: '2', merge_condition_model:'AND'}
                                                );
                                                return conditions;
                                            },
                                            confirm : function(selectItem){
                                                dialog.exception_reason = selectItem.page_no;
                                                dialog.exception_reason_display = selectItem.page_no + ' / '+selectItem.page_name;
                                            }
                                        });
                                    },
                                    // 20200214 modify by WeiTing for M#65970 - 取得叫修原因資料。
                                    callRepairReasonInfoGet: function(callback){
                                        $MMWService.pageNumberInfoGet({
                                            // page_mode: 'injection_mould_reason_info_get',
                                            form_name:'PageNo_69',
                                            query_condition_info:[
                                                {column_name:'ReasonType', query_mode:0, value: '1', merge_condition_model:'AND'},
                                                {column_name:'ISSUESTATE', query_mode:0, value: '2', merge_condition_model:'AND'}
                                            ]
                                        }, function(itemList){
                                            if(callback)
                                                callback(itemList);
                                        });
                                    },
                                    // 20200214 modify by WeiTing for M#65970 - 點擊直接叫修按鈕，可以馬上選擇叫修原因。
                                    callRepairReason: function(){
                                        if(!dialog.isRepair){
                                            dialog.isRepair = true;
                                            dialog.callRepairReasonInfoGet(function(itemList){
                                                $rootScope.showSelect({
                                                    label: 'cavity_reason_name',
                                                    code: 'cavity_reason_no',
                                                    selectCode: dialog.call_repair_reason || '',
                                                    list: itemList,
                                                    confirm: function (item, radioDialog) {
                                                        radioDialog.hide();
                                                        dialog.call_repair_reason_no = item.cavity_reason_no;
                                                        dialog.call_repair_reason_name = item.cavity_reason_name;
                                                    }
                                                });
                                            });
                                        } else {
                                            dialog.isRepair = false;
                                            dialog.call_repair_reason_no = '';
                                            dialog.call_repair_reason_name = '';
                                        };
                                    },
                                    // 20200214 modify by WeiTing for M#65970 - 增加叫修原因開窗。
                                    loadCallRepairReason: function(){
                                        if(dialog.isRepair){
                                            $rootScope.showQueryList({
                                                key:'cavity_reason_no',
                                                header: [
                                                    {label: $filter('translate')('common.queryList.reason_c'), code:'cavity_reason_no'},
                                                    {label: $filter('translate')('common.queryList.reason_n'), code:'cavity_reason_name'}
                                                ],
                                                query: function(conditions, feedback){
                                                    dialog.callRepairReasonInfoGet(function(itemList){
                                                        feedback(itemList);
                                                    });
                                                },
                                                selectCode: dialog.call_repair_reason || '',
                                                confirm: function(selectItem, dialogType){
                                                    dialogType.hide();
                                                    dialog.call_repair_reason_no = selectItem.cavity_reason_no;
                                                    dialog.call_repair_reason_name = selectItem.cavity_reason_name;
                                                }
                                            });
                                        } else {
                                            $rootScope.showAlert($filter('translate')('B0221.exception.msg.column01'));
                                        };
                                    },
                                    confirm:function(){
                                        if(dialog.exception_reason_display == ''){
                                            $rootScope.showAlert($filter('translate')('B0221.msg.exception_reason_empty'));
                                        } else {
                                            // 20200214 modify by WeiTing for M#65970 - 如果使用者有打勾叫修原因按鈕，但沒有選擇叫修原因，不讓資料送回。
                                            if(dialog.isRepair && dialog.call_repair_reason_name == ''){
                                                $rootScope.showAlert($filter('translate')('B0221.exception.msg.please_select_call_repair_reason'));
                                            } else {
                                                var content = {
                                                    next_date:moment(dialog.next_date).format('YYYY-MM-DD'),
                                                    reason_no:dialog.exception_reason,
                                                    scrap_details:[],
                                                    // 20200214 modify by WeiTing for M#65970 - 多回傳叫修原因編號(call_repair_reason_no)。
                                                    call_repair_reason_no: dialog.call_repair_reason_no
                                                };
                                                exception_acc_details.forEach(function(detail){
                                                    content.scrap_details.push({accessory_no:detail.AccessoryNo, machine_no: options.machine.machine_no});
                                                });
                                                funUnloadAccessory_Scrap(content, function(){
                                                    dialog.hide();
                                                    if(callback)
                                                        callback();
                                                });
                                            }
                                        }
                                    }
                                };
                            }
                        );
                    })
                }

                function openDialog(unLoadAccDetails, LoadAccDetails){
                    $mddProvider.UseForm('B0221', ()=>{
                        $mdDialog.dialog(
                            'module/moduleB/program/B02/B0221/B0221.html',
                            function (dialog) {
                                return {
                                    css_url:'module/moduleB/program/B02/B0221/B0221.css',
                                    machine : options.machine,
                                    parent: options.parent,
                                    LoadAccDetails:LoadAccDetails,
                                    unLoadAccDetails:unLoadAccDetails,
                                    IsAutoUnload:'N',
                                    unLoadAcc_barcode: '', //下模條碼
                                    loadAcc_barcode: '', //上模條碼
                                    Details_PageConfig: {page : 0, page_count : 8}, //已上機模具分頁設定
                                    uDetails_PageConfig: {page : 0, page_count : 8}, //可用模具分頁設定
                                    Details_filterValue: {},
                                    uDetails_filterValue: {},
                                    _event:{
                                        'unLoadAcc_barcode':{
                                            change: (unLoadAcc_barcode, event)=>{
                                                dialog.input_unLoad_accessory(event)
                                            }
                                        },
                                        'loadAcc_barcode':{
                                            change: (loadAcc_barcode, event)=>{
                                                dialog.input_load_accessory(event)
                                            }
                                        },
            	                        // 20230222 add by Alan for M#128270: 補上disabled
                                        'dialog.list_LoadAccDetails.TargetLocator': {
                                            disabled: (modelName, args)=>{
                                                return args.eventScope.isSelect ? 'N' : 'Y';
                                            }
                                        },
                                    },
                                    _fn:{
                                        'UnLoadAccException':{
                                            click: ()=>{
                                                dialog.UnLoadAccException()
                                            }
                                        },
                                        'UnLoadAccFromEquipment':{
                                            click:()=>{
                                                dialog.UnLoadAccFromEquipment()
                                            }
                                        },
                                        'LoadAccToEquipment':{
                                            click: ()=>{
                                                dialog.LoadAccToEquipment()
                                            }
                                        }
                                    },
                                    clickIsAutoUnload: function(){
                                        if(dialog.IsAutoUnload == 'Y'){
                                            dialog.IsAutoUnload = 'N';
                                        } else {
                                            dialog.IsAutoUnload = 'Y';
                                        }
                                    },
                                    LoadAccDetails_SelectAll:function(){
                                        dialog.LoadAccDetails_isSelectAll = !dialog.LoadAccDetails_isSelectAll;
                                        dialog.LoadAccDetails.forEach(function(detail){
                                            detail.isSelect = dialog.LoadAccDetails_isSelectAll;
                                        });
                                    },
                                    unLoadAccDetails_SelectAll:function(){
                                        dialog.unLoadAccDetails_isSelectAll = !dialog.unLoadAccDetails_isSelectAll;
                                        dialog.unLoadAccDetails.forEach(function(detail){
                                            detail.isSelect = dialog.unLoadAccDetails_isSelectAll;
                                        });
                                    },
                                    back: function () {
                                        dialog.hide();
                                        if(options.callback)
                                            options.callback();
                                    },
                                    LoadAccToEquipment: function () {//上架
                                        //20220308 add by 翰鴻 for M#102577
                                        $mddProvider.UseForm('B0221_loadtime', ()=>{
                                            $mdDialog.dialog(
                                                // 'JSplugins/angular-material-lite/template/dynamicService.tmp.html',
                                                'module/moduleB/program/B02/B0221/LoadTime.html',
                                                function (LoadTimeDialog) {
                                                    return {                                                
                                                        ACLoadTime: 0,
                                                        css_url:'module/moduleB/program/B02/B0221/B0221_UnloadTimeAndLocator.css',
                                                        _fn:{
                                                            'Execute':{
                                                                click:()=>{
                                                                    LoadTimeDialog.confirm()
                                                                }
                                                            }
                                                        },   
                                                        _event:{
                                                            'ACLoadTime':{  //	20231205 modify by 0148557: 厦晖：模具实际上模、下模输入数字是没有小数点
                                                                click:(labor,{event})=>{
                                                                    var target = event.currentTarget;
                                                                    angular.element(target).addClass('is-focus');
                                                                    $calculater.open(function(num,calcDialog){
                                                                        calcDialog.hide();
                                                                        angular.element(target).removeClass('is-focus');
                                                                        LoadTimeDialog.ACLoadTime = num; 
                                                                    },
                                                                    function(calcDialog){
                                                                        angular.element(target).removeClass('is-focus');
                                                                        calcDialog.hide();
                                                                    },
                                                                    {
                                                                        defaultNum : labor,
                                                                        decimal : true
                                                                    });
                                                                }
                                                            }
                                                        },                                           
                                                        back: function () {
                                                            
                                                            this.hide();
                                                        },
                                                        confirm: function () {
                                                            if (Number(LoadTimeDialog.ACLoadTime) > 9999)
                                                            {
                                                                $rootScope.showAlert($filter('translate')('B0221.exception.loadTimeAlert'));
                                                                return;
                                                            }

                                                            //檢核是否有勾選
                                                            var content = {
                                                                IsAutoUnload: dialog.IsAutoUnload,
                                                                pTable: [],
                                                                ACLoadTime: LoadTimeDialog.ACLoadTime
                                                            };
                                                            dialog.unLoadAccDetails.forEach(function (detail) {
                                                                if (detail.isSelect) {
                                                                    content.pTable.push({
                                                                        EQUIPMENTTYPE: dialog.machine.machine_type,
                                                                        EQUIPMENTNO: dialog.machine.machine_no,
                                                                        ACCESSORYTYPE: detail.AccessoryType,
                                                                        ACCESSORYNO: detail.AccessoryNo,
                                                                        USERNO: config.cache.account,
                                                                        STARTTIME: moment(new Date()).format('YYYY-MM-DD'),
                                                                        ACCESSORYVERSION: detail.AccessoryVersion,
                                                                        ACCSerialNo: detail.ACCSerialNo,
                                                                        CHANGETOOL: detail.CHANGETOOL                                                                
                                                                    });
                                                                }
                                                            });

                                                            if (content.pTable.length != 0) {
                                                                LoadAccToEquipment(content, function () {
                                                                    //重新取得畫面上的資料
                                                                    getAccessoryData(function (unLoadAccDetails, LoadAccDetails) {
                                                                        dialog.LoadAccDetails_isSelectAll = false;
                                                                        dialog.unLoadAccDetails_isSelectAll = false;
                                                                        dialog.unLoadAccDetails = unLoadAccDetails;
                                                                        dialog.LoadAccDetails = LoadAccDetails;
                                                                        dialog.loadAcc_barcode = '';
                                                                    });
                                                                });
                                                            } else {
                                                                $rootScope.showAlert($filter('translate')('B0221.msg.LoadAccDetails_empty'));
                                                            }

                                                            this.hide();
                                                        }
                                                    }
                                                }
                                            )
                                        })


                                        
                                    },
                                    UnLoadAccFromEquipment: function () {
                                        //20220418 add by 翰鴻 for M#110096
                                        //修正 檢查是否有勾選資料
                                        var selectedCount = $.grep(dialog.LoadAccDetails, function (n, i) {
                                            return n.isSelect;
                                        }).length;
                                        if (selectedCount == 0)
                                        {
                                            $rootScope.showAlert($filter('translate')('B0221.msg.unLoadAccDetails_empty'));
                                        }
                                        else
                                        {
                                            //20220309 modify by 翰鴻 for M#102577
                                            $mddProvider.UseForm('B0221_UnloadTimeAndLocator', ()=>{
                                                $mdDialog.dialog(
                                                    'module/moduleB/program/B02/B0221/UnloadTimeAndLocator.html',
                                                    function (unLoadTimeDialog) {
                                                        return {
                                                            css_url:'module/moduleB/program/B02/B0221/B0221_UnloadTimeAndLocator.css',
                                                            ACUnLoadTime: 0,
                                                            _fn:{
                                                                'Execute':{
                                                                    click:()=>{
                                                                        unLoadTimeDialog.confirm()
                                                                    }
                                                                }
                                                            },
                                                            _event:{   //	20231205 modify by 0148557: 厦晖：模具实际上模、下模输入数字是没有小数点
                                                                'ACUnLoadTime':{
                                                                    click:(labor,{event})=>{
                                                                        var target = event.currentTarget;
                                                                         angular.element(target).addClass('is-focus');
                                                                        $calculater.open(function(num,calcDialog){
                                                                            calcDialog.hide();
                                                                             angular.element(target).removeClass('is-focus');
                                                                             unLoadTimeDialog.ACUnLoadTime = num;
                                                                             
                                                                        },
                                                                        function(calcDialog){
                                                                            angular.element(target).removeClass('is-focus');
                                                                            calcDialog.hide();
                                                                        },
                                                                        {
                                                                            defaultNum : labor,
                                                                            decimal : true
                                                                        });
                                                                    }
                                                                }
                                                            },
                                                            back: function () {

                                                                this.hide();
                                                            },
                                                            confirm: function () {
                                                                if (Number(unLoadTimeDialog.ACUnLoadTime) > 9999) {
                                                                    $rootScope.showAlert($filter('translate')('B0221.exception.unloadTimeAlert'));
                                                                    return;
                                                                }

                                                                //檢核是否有勾選
                                                                var content = {
                                                                    pTable: [],
                                                                    ACUnLoadTime: unLoadTimeDialog.ACUnLoadTime,
                                                                    TempLocatorNo: unLoadTimeDialog.TempLocatorNo
                                                                };
                                                                dialog.LoadAccDetails.forEach(function (detail) {
                                                                    if (detail.isSelect) {
                                                                        content.pTable.push({
                                                                            EQUIPMENTTYPE: dialog.machine.machine_type,
                                                                            EQUIPMENTNO: dialog.machine.machine_no,
                                                                            ACCESSORYTYPE: detail.AccessoryType,
                                                                            ACCESSORYNO: detail.AccessoryNo,
                                                                            USERNO: config.cache.account,
                                                                            STARTTIME: moment(new Date()).format('YYYY-MM-DD'),
                                                                            ACCESSORYVERSION: detail.AccessoryVersion,
                                                                            ACCSerialNo: detail.ACCSerialNo,
                                                                            TempLocatorNo: detail.TargetLocator
                                                                        });
                                                                    }
                                                                });

                                                                if (content.pTable.length != 0) {
                                                                    UnLoadAccFromEquipment(content, function () {
                                                                        //重新取得畫面上的資料
                                                                        getAccessoryData(function (unLoadAccDetails, LoadAccDetails) {
                                                                            dialog.LoadAccDetails_isSelectAll = false;
                                                                            dialog.unLoadAccDetails_isSelectAll = false;
                                                                            dialog.unLoadAccDetails = unLoadAccDetails;
                                                                            dialog.LoadAccDetails = LoadAccDetails;
                                                                            dialog.unLoadAcc_barcode = '';
                                                                        });
                                                                    });
                                                                } else {
                                                                    $rootScope.showAlert($filter('translate')('B0221.msg.unLoadAccDetails_empty'));
                                                                }

                                                                this.hide();
                                                            },
                                                            beforeShown: function () {
                                                                $MMWService.sendToServer({
                                                                    // uri: $mddProvider.Getfunc('B0221', 'acunloadtime_compute_info_get'),
                                                                    uri: 'ServicesSTD.Module_ACC.acunloadtime_compute_info_get',
                                                                    content: {
                                                                        equipmentno: options.machine.machine_no
                                                                    },
                                                                    success: function (data) {
                                                                        unLoadTimeDialog.ACUnLoadTime = data.ACunloadtime;
                                                                    }
                                                                });
                                                            }
                                                        }
                                                    }
                                                )
                                            })
                                        }
                                        


                                        
                                    },
                                    UnLoadAccException:function(){
                                        //20180904 add by Dustdusk for M#46551:增加異常下模功能
                                        //檢核是否有勾選
                                        var exception_acc_details = [];
                                        dialog.LoadAccDetails.forEach(function(detail){
                                            if(detail.isSelect){
                                                exception_acc_details.push(detail);
                                            }
                                        });

                                        if(exception_acc_details.length != 0){
                                            openExceptionDialog(exception_acc_details, function(){
                                                //重新取得畫面上的資料
                                                getAccessoryData(function(unLoadAccDetails, LoadAccDetails){
                                                    dialog.LoadAccDetails_isSelectAll = false;
                                                    dialog.unLoadAccDetails_isSelectAll = false;
                                                    dialog.unLoadAccDetails = unLoadAccDetails;
                                                    dialog.LoadAccDetails = LoadAccDetails;
                                                });
                                            });
                                        } else {
                                            $rootScope.showAlert($filter('translate')('B0221.msg.ExceptionAccDetails_empty'));
                                        }
                                    },
                                    input_unLoad_accessory: function(event){
                                        //條碼下模
                                        if(event.keyCode == 13){
                                            unLoad_accessory_callback(dialog);
                                        }
                                    },
                                    input_load_accessory: function(event){
                                        //條碼上模
                                        if(event.keyCode == 13){
                                            load_accessory_callback(dialog);
                                        }
                                    },
                                    qrcode_unLoad_accessory: function(event){
                                        //下模QRCode輸入
                                        OpenScanner(function(result){
                                            dialog.unLoadAcc_barcode = result.text;
                                            unLoad_accessory_callback(dialog);
                                        });
                                    },
                                    qrcode_load_accessory: function(event){
                                        //上模QRCode輸入
                                        OpenScanner(function(result){
                                            dialog.loadAcc_barcode = result.text;
                                            load_accessory_callback(dialog);
                                        });
                                    },
                                    // 20230505 modify by 文蔚 for #134693: 修正過濾功能
                                    getPages: function(list, PageConfig, filterItem){
                                        //分頁
                                        if(list){
                                            return temp_list = list.filter(function(item, index, array){
                                                var isOK = true,
                                                    keys = Object.keys(filterItem);
                                                for(var i =0; i<keys.length;i++){
                                                    var key = keys[i];
                                                    if(isOK && item[key]!=null && filterItem[key] && filterItem[key]!=''){
                                                        if(item[key].indexOf(filterItem[key]) == -1){
                                                            isOK = false;
                                                            break;
                                                        }
                                                    }
                                                }
                                                return isOK;
                                            });
                                        } else {
                                            return list;
                                        }
                                    }
                                };
                            }
                        );
                    })
                }

                function getAccessoryData(callback){
                    var unLoadAcc_content = {
                        EquipmentType: options.machine.machine_type,
                        EQUIPMENTNO: options.machine.machine_no,
                        ProductList: options.ProductList
                    };
                    var unLoadAccDetails = [], LoadAccDetails = [];
                    LoadAppliedAccessory(unLoadAcc_content, function(data){
                        if(data[0].AccessoryNo !==null)
                            unLoadAccDetails = data;

                        var LoadAcc_content = {
                            EQUIPMENTTYPE: options.machine.machine_type,
                            EQUIPMENTNO : options.machine.machine_no
                        };
                        LoadOnEquipmentAccessory(LoadAcc_content, function(data){
                            if(data[0].AccessoryNo !==null)
                                LoadAccDetails = data;
                            if(callback)
                                callback(unLoadAccDetails,LoadAccDetails);
                        });
                    });
                }
                
                getAccessoryData(function(unLoadAccDetails, LoadAccDetails){
                    if(unLoadAccDetails.length !=0 || LoadAccDetails.length !=0){
                        openDialog(unLoadAccDetails, LoadAccDetails);
                    } else {
                        $rootScope.showAlert($filter('translate')('common.msg.no_data'));
                    }
                });
                
            };
        }]);
});
